package com.rain.shoppingscienceproj.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.module.Address;
import com.rain.shoppingscienceproj.module.Goods;
import com.rain.shoppingscienceproj.module.MainData;
import com.rain.shoppingscienceproj.ui.utile.TypeData;
import com.rain.shoppingscienceproj.ui.view.MyGridView;
import com.rain.shoppingscienceproj.utils.ImageUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Rain
 * Create: 2018/9/1 14:20
 */
public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.MyViewHolder> {

    private Context mContext;
    private int type;
    private List<Goods> list;
    private boolean isShow = false;
    private ItemClickListener itemClickListener;
    private LongItemListener longItemListener;
    private ItemKucunClickListener itemKucunClickListener;
    private List<MainData> listMain = TypeData.getData();

    public void setItemKucunClickListener(ItemKucunClickListener itemKucunClickListener) {
        this.itemKucunClickListener = itemKucunClickListener;
    }

    public void setLongItemListener(LongItemListener longItemListener) {
        this.longItemListener = longItemListener;
    }

    public GoodsAdapter(Context mContext, int type) {
        this.mContext = mContext;
        this.type = type;
    }

    public boolean isShow() {
        return isShow;
    }

    public void setShow(boolean show) {
        isShow = show;
    }

    public List<Goods> getList() {
        return list;
    }

    public void setList(List<Goods> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setType(int type) {
        this.type = type;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (type == 1) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_horizontal_layout, parent, false);//解决宽度不能铺满
            MyViewHolder holder = new MyViewHolder(view);
            return holder;
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_grid_layout, parent, false);//解决宽度不能铺满
            MyViewHolder holder = new MyViewHolder(view);
            return holder;
        }
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Goods goods = list.get(position);
        ImageUtils.imageRound(mContext, goods.getGoodsImage(), holder.iv_image);
        holder.tv_title.setText(goods.getGoodsName());
        holder.tv_money.setText(Double.valueOf(goods.getGoodsPay()) / 100 + "");
        holder.tv_person_num.setText(goods.getGoodsNum() + "付款");
        for (MainData mainData : listMain) {
            if (goods.getProvinceId().equals(mainData.getId())) {
                holder.tv_address.setText("地址:" + mainData.getTitle());
            }
        }
        holder.ll_item.setOnClickListener(view -> {
            itemClickListener.ItemClickListener(goods.getGoodsId());
        });
        if (type == 1) {
            if (isShow) {
                holder.tv_upKucun.setVisibility(View.VISIBLE);
                holder.ll_item.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        longItemListener.LongItemLister(goods, position);
                        return false;
                    }
                });
                holder.tv_upKucun.setOnClickListener(v -> itemKucunClickListener.itemKucunClickListener(goods.getGoodsId()));
            }
        }

    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        } else {
            return list.size();
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.ll_item)
        LinearLayout ll_item;

        @BindView(R.id.iv_image)
        ImageView iv_image;

        @BindView(R.id.tv_title)
        TextView tv_title;

        @BindView(R.id.tv_money)
        TextView tv_money;

        @BindView(R.id.tv_person_num)
        TextView tv_person_num;

        @BindView(R.id.tv_address)
        TextView tv_address;

        @BindView(R.id.ll_enter_store)
        LinearLayout ll_enter_store;

        @BindView(R.id.tv_upKucun)
        TextView tv_upKucun;


        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface ItemClickListener {
        void ItemClickListener(String shopId);
    }

    public interface ItemKucunClickListener {
        void itemKucunClickListener(String goodsId);
    }

    public interface LongItemListener {
        void LongItemLister(Goods goods, int position);
    }
}
